<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :okAuth="'maintenance:management:maintenanceWorkTask:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <div class="fireVeIm">
      <div  class="fireImg">
        <span style="margin-right: 5px;">
          异常照片：
        </span>
        <Image v-if="abnormalPic" :src='abnormalPic' width="auto" height="100%"></Image>
        <img v-else src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
      </div>

      <div  class="fireImg">
        <span style="margin-right: 5px;">
          处置完成照片：
        </span>
        <Image v-if='completeUrl' :src="completeUrl" width="auto" height="100%"></Image>
        <img v-else src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
      </div>
    </div>
    <BasicForm @register="registerForm" />
    <BpmRuntimeTraceTimeLine
      :formKey= "'maintenance_process'"
      :bizKey=record.taskId
    />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsMaintenanceManagementMaintenanceWorkTaskForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { MaintenanceWorkTask, maintenanceWorkTaskSave, maintenanceWorkTaskForm } from '/@/api/maintenance.management/maintenanceWorkTask';
  import { Image } from 'ant-design-vue';
  import  BpmRuntimeTraceTimeLine  from '/@/components/Bpm/src/BpmRuntimeTraceTimeLine.vue'; 



  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('maintenance.management.maintenanceWorkTask');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<MaintenanceWorkTask>({} as MaintenanceWorkTask);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('详情') : t('详情'),
  }));

  const abnormalPic = ref('')
  const completeUrl = ref('')

  const inputFormSchemas: FormSchema[] = [
    // {
    //   label: t('异常图片'),
    //   field: 'abnormalPic',
    //   component: 'Image',
    //   componentProps: {
    //     disabled: true
    //   }
    // },
    // {
    //   label: t('完成图片URL'),
    //   field: 'completeUrl',
    //   component: 'Image',
    //   componentProps: {
    //     disabled: true
    //   }
    // },
    {
      label: t('所属企业名称'),
      field: 'enterpriseName',
      component: 'TreeSelect',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('设备名称'),
      field: 'equipmentName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('工单名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('地点'),
      field: 'address',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('维保企业名称'),
      field: 'maintenanceName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('维保企业编码'),
      field: 'maintenanceCode',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('隐患描述'),
      field: 'remark',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('上报人'),
      field: 'reportPersonName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('上报人编码'),
      field: 'reportPersonCode',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('上报时间'),
      field: 'reportTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
        disabled: true
      },
    },
    {
      label: t('区域名称'),
      field: 'regionName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('执行人'),
      field: 'executePerson',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('执行人手机号码'),
      field: 'executePersonPhone',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('反馈描述'),
      field: 'feedbackRemark',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await maintenanceWorkTaskForm(data);
    record.value = (res.maintenanceWorkTask || {}) as MaintenanceWorkTask;
    record.value.__t = new Date().getTime();
    abnormalPic.value = record.value.abnormalPic || '';
    completeUrl.value = record.value.completeUrl || '';
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };
      // console.log('submit', params, data, record);
      const res = await maintenanceWorkTaskSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
<style scoped lang="less"> 
.fireVeIm{
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 5%;
}
.fireImg {
  width: 40%;
  height: 200px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
</style>